// import { produce } from 'immer'

const baseState = {
  ids: [1, 2],
  pos: {
    x: 1,
    y: 1
  }
}

let nextState = produce(baseState, (draft) => {
  draft.ids.push(3)
})

console.log(nextState, baseState)
console.log('nextState === baseState', nextState === baseState)
console.log('ids ', nextState.ids === baseState.ids)
console.log('pos ', nextState.pos === baseState.pos)


function produce(baseState, producer) {
  let draft = JSON.parse(JSON.stringify(baseState)) // 深拷贝
  producer(draft)
  const nextState = {}
  for (let key in baseState) {
    if(JSON.stringify(baseState[key]) !== JSON.stringify(draft[key])) {
      nextState[key] = draft[key]
    } else {
      nextState[key] = baseState[key]
    }
  }
  return nextState
}
